from lxml import etree
import requests
import csv

head = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36'}

def get_html(url,head):
    try:
        r = requests.get(url,headers=head)
        r.encoding = r.apparent_encoding
        r.raise_for_status()
        return r.text
    except Exception as err:
        print(err)
def parser(html):
    doc = etree.HTML(html)
    out_list=[]
    for row in doc.xpath('//*[@id="tab-book"]/div[2]/div[3]/div/ul/li/div[2]/h4/a'):
        row_data=[
            row.xpath('//*[@id="tab-book"]/div[2]/div[3]/div/ul/li/div[2]/h4/a'),
            row.xpath('//*[@id="tab-book"]/div[2]/div[3]/div/ul/li/div[2]/div')[0],
            row.xpath('//*[@id="tab-book"]/div[2]/div[3]/div/ul/li/div[2]/span/span')[0]
        ]
        out_list.append(row_data)
    return out_list
def save_csv(item,path):
    with open(path,'a+',newline='',encoding='utf-8')as f:
        scv_write=csv.writer(f)
        scv_write.writerow(item)
if __name__ == '__main__':
    for i in range(1,6):
        url = 'https://www.ryjiaoyu.com/book'
        html = get_html(url)
        out_list = parser(html)
        save_csv('asa.csv',out_list)

